import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/models/response/message_comment_response.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:sainbainu/common/widgets/widget.dart';

import 'index.dart';

class CommentPage extends GetView<CommentController> {
  const CommentPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<CommentController>(
      init: CommentController(),
      builder: (_) {
        return Scaffold(
          body: EasyRefresh.builder(
            controller: _.state.refreshController,
            onRefresh: _.onRefresh,
            onLoad: _.onRichBottom,
            childBuilder: (context, physics) {
              return _buildList(physics);
            },
          ),
        );
      },
    );
  }

  Widget _buildList(ScrollPhysics physics) {
    return GetBuilder<CommentController>(
      id: 'commentList',
      builder: (_) => ListView.separated(
        physics: physics,
        itemCount: _.state.list.rows.length,
        separatorBuilder: (BuildContext context, int index) {
          return const SizedBox(height: 1);
        },
        itemBuilder: (BuildContext context, int index) {
          MessageCommentResponse item = _.state.list.rows[index];
          return _buildListItem(item, _);
        },
      ),
    );
  }

  Widget _buildListItem(MessageCommentResponse item, CommentController _) {
    return GestureDetector(
      onTap: () => _.toContentDetailPage(item),
      child: Container(
        color: Colors.white,
        padding: const EdgeInsets.symmetric(
          vertical: 10,
          horizontal: 15,
        ),
        child: UserInfoCell(
          avatarHeroTag: '${item.uniqueTag}_${item.noteId}',
          name: item.customerName ?? '',
          avatar: item.avatar,
          role: item.operateMsg,
          showNameAuth: item.officialFlag == 1,
          roleFontSize: 13,
          rightChild: _buildItemRight(item),
          bottomChild: _buildItemBottom(item),
          onUserTap: () => _.toUserDetailPage(item.customerId),
        ),
      ),
    );
  }

  Widget _buildItemRight(MessageCommentResponse item) {
    if (item.text != null && item.text!.isNotEmpty) {
      return SizedBox(
        width: 100,
        child: Text(
          item.text!,
          maxLines: 2,
          overflow: TextOverflow.ellipsis,
          textAlign: TextAlign.end,
          style: const TextStyle(color: AppColors.unactive3, fontSize: 12),
        ),
      );
    } else if (item.image != null && item.image!.isNotEmpty) {
      return SizedBox(
        width: 50,
        height: 50,
        child: ClipRRect(
          borderRadius: BorderRadius.circular(6),
          child: CustomImage(url: item.image!),
        ),
      );
    } else if (item.video != null && item.video!.isNotEmpty) {
      return const Text(
        '[视频内容]',
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        textAlign: TextAlign.end,
        style: TextStyle(color: AppColors.unactive3, fontSize: 12),
      );
    } else if (item.audio != null && item.audio!.isNotEmpty) {
      return const Text(
        '[语音内容]',
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        textAlign: TextAlign.end,
        style: TextStyle(color: AppColors.unactive3, fontSize: 12),
      );
    } else {
      return const SizedBox.shrink();
    }
  }

  Widget _buildItemBottom(MessageCommentResponse item) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (item.commentText != null && item.commentText!.isNotEmpty)
          Padding(
            padding: const EdgeInsets.only(top: 4),
            child: ContentCardText(item.commentText!),
          ),
        if (item.commentImage != null && item.commentImage!.isNotEmpty)
          Padding(
            padding: const EdgeInsets.only(top: 6),
            child: SizedBox(
              width: 50,
              height: 50,
              child: ClipRRect(
                borderRadius: BorderRadius.circular(6),
                child: CustomImage(url: item.commentImage!),
              ),
            ),
          ),
        if (item.commentAudio != null && item.commentAudio!.isNotEmpty)
          Padding(
            padding: const EdgeInsets.only(top: 6),
            child: AudioWaveWidget(
              duration: item.commentAudioDuration ?? 0,
            ),
          ),
      ],
    );
  }
}
